package bairui.international.weixin.portal.miniapp.controller.position.position;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.weixin.portal.miniapp.util.ElasticSearchDeal;
import bairui.international.weixin.portal.miniapp.util.ElasticSearchDeal.Output;
import bairui.international.weixin.portal.miniapp.util.ElasticSearchDeal.OutputRecord;

/**
 *
 * 搜索
 *
 * @generated
 * 
 * @see STest_C_Position_Search_Get 测试
 *
 */
@Controller
public class C_Position_Search_Get
 extends SC_Position_Search_Get 
{
	
	@Autowired(required=false) private RestHighLevelClient client;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, @RequestParam(required=false) String[] params
	) throws Exception {
		if(null == params || params.length<=0){
			return new Output(new ArrayList<OutputRecord>(), 0, 0);
		}
		// 查询
		return ElasticSearchDeal.query(
				client, 
				booleanBuilder->{
					for(String para : params){
						if(null==para || "".equals(para.trim())){
							continue;
						}
						booleanBuilder
//							.must(QueryBuilders.multiMatchQuery(
//									"*" + para.trim() + "*", 
//									ElasticSearchDeal.Field.Name,
//									ElasticSearchDeal.Field.CityName,
//									ElasticSearchDeal.Field.ProvinceName
//								));
							.must(QueryBuilders.wildcardQuery(ElasticSearchDeal.Field.Search, "*" + para.trim() + "*"));
					}
				}, 
				new ElasticSearchDeal.Page(0, 10000), 
				new ElasticSearchDeal.Sort(ElasticSearchDeal.Field.OrderValue, SortOrder.DESC)
			);
	}
	
}
